using System;
using System.Data.Common;
using System.Runtime.Serialization;
using System.Text.RegularExpressions;
using Oracle.ManagedDataAccess.Client;

namespace OracleInternal.Network
{
	[Serializable]
	internal class NetworkException : DbException
	{
		internal int m_errorCode;

		public new int ErrorCode => m_errorCode;

		public int Number => m_errorCode;

		internal NetworkException(int errorCode)
			: base(OracleStringResourceManager.GetErrorMesg(errorCode), errorCode)
		{
			m_errorCode = errorCode;
		}

		internal NetworkException(int errorCode, Exception inner)
			: base(OracleStringResourceManager.GetErrorMesg(errorCode), inner)
		{
			m_errorCode = errorCode;
		}

		internal NetworkException(int errorCode, params object[] inpParams)
			: base(sprintf(OracleStringResourceManager.GetErrorMesg(errorCode), inpParams))
		{
			m_errorCode = errorCode;
		}

		protected NetworkException(SerializationInfo info, StreamingContext context)
			: base(info, context)
		{
		}

		internal static string sprintf(string input, params object[] inpVars)
		{
			int i = 0;
			input = Regex.Replace(input, "%.", (Match m) => "{" + i++ + "}");
			return string.Format(input, inpVars);
		}
	}
}
